package com.adou.Controller;

import com.adou.common.util.UtilPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

@RestController
public class DownLoadController {

    @Autowired
    private HttpServletRequest request;

    /**
     * 文件下载
     * @param f 文件名称
     * @return  返回数据流
     * @throws IOException
     */
    @RequestMapping(value = "upload/{f:.+}",method = RequestMethod.GET)
    public ResponseEntity<InputStreamResource>  download(@PathVariable String f) throws IOException {
        String filePath = UtilPath.getRootPath() + "/upload/";
        FileSystemResource file = new FileSystemResource(filePath + f);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getFilename()));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }

    /**
     * 下载流程附件
     * @param f
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "upload/file/{f:.+}",method = RequestMethod.GET)
    public ResponseEntity<InputStreamResource>  downloadFile(@PathVariable String f, String filename) throws IOException {
        String filePath = UtilPath.getRootPath() + "upload/file/";
        FileSystemResource file = new FileSystemResource(filePath + f);
        filename = URLDecoder.decode(filename,"utf-8");
        String agent = request.getHeader("User-Agent").toLowerCase();
        String filenameEncoder;
        if (agent.contains("msie")) {
            // IE浏览器
            filenameEncoder = URLEncoder.encode(filename, "utf-8");
            filenameEncoder = filenameEncoder.replace("+", " ");
        } else if (agent.contains("firefox")) {
            // 火狐浏览器
            String fn = new String(Base64Utils.encode(filename.getBytes("utf-8")));
            filenameEncoder = "=?utf-8?B?" + fn + "?=";
        } else {
            // 其它浏览器
            filenameEncoder = URLEncoder.encode(filename, "utf-8");
        }

        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", filenameEncoder));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }
}
